<?php


class AdminController extends Controller
{
  /**
   * 初始化方法，检测登陆
   */
  public function init()
  {
    $this->checkUserLogin(3);
  }


  /**
   * 后台主页
   */
  public function actionIndex()
  {
    $this->render('index');
  }

  /**
   * 注销登录
   */
  public function actionLogout()
  {
    Yii::app()->user->logout();
    $this->redirect(Yii::app()->createUrl('site/index'));
  }

  /**
   * 教师列表
   * @param string $keywords 搜索关键字
   */
  public function actionTeacher($keywords = '')
  {
    $this->iCheck();
    $data = UserService::instance()->getList(2,20, $keywords);
    $this->render('teacher/list', array(
        'data' => $data
    ));
  }

  /**
   * 添加/编辑教师信息
   * @param int $id
   */
  public function actionPost_teacher($id = 0)
  {
    if (Yii::app()->request->isPostRequest) {
      $result = UserService::instance()->post($this->filter($_POST));
      if ($result === true) {
        $this->jsMsg('操作成功!', Yii::app()->createUrl('admin/teacher'));
      }else{
        $this->jsMsg($this->getError($result));
      }
    } else {
      if ($id > 0) {
        //编辑
        $data = User::model()->findByPk($id);
        $this->render('teacher/post', array(
            'data' => $data
        ));
      } else {
        //添加
        $this->render('teacher/post');
      }
    }
  }

  /**
   * 设置教师状态
   * @param int $state
   */
  public function actionTeacher_gateway($state = 0)
  {
    echo UserService::instance()->setState($_POST['list'],$state)?1:0;
  }


  /**
   * 学生列表
   * @param string $keywords 搜索关键字
   */
  public function actionStudent($keywords = '')
  {
    $this->iCheck();
    $data = UserService::instance()->getList(1,20, $keywords);
    $this->render('student/list', array(
        'data' => $data
    ));
  }

  /**
   * 添加/编辑学生信息
   * @param int $id
   */
  public function actionPost_student($id = 0)
  {
    if (Yii::app()->request->isPostRequest) {
      $result = UserService::instance()->post($this->filter($_POST));
      if ($result === true) {
        $this->jsMsg('操作成功!', Yii::app()->createUrl('admin/student'));
      }else{
        $this->jsMsg($this->getError($result));
      }
    } else {
      if ($id > 0) {
        //编辑
        $data = User::model()->findByPk($id);
        $this->render('student/post', array(
            'data' => $data
        ));
      } else {
        //添加
        $this->render('student/post');
      }
    }
  }

  /**
   * 设置学生状态
   * @param int $state
   */
  public function actionStudent_gateway($state = 0)
  {
    echo UserService::instance()->setState($_POST['list'],$state)?1:0;
  }


  /**
   * 学生列表
   * @param string $keywords 搜索关键字
   */
  public function actionAdmin($keywords = '')
  {
    $this->iCheck();
    $data = UserService::instance()->getList(3,20, $keywords);
    $this->render('admin/list', array(
        'data' => $data
    ));
  }

  /**
   * 添加/编辑管理员信息
   * @param int $id
   */
  public function actionPost_admin($id = 0)
  {
    if (Yii::app()->request->isPostRequest) {
      $result = UserService::instance()->post($this->filter($_POST));
      if ($result === true) {
        $this->jsMsg('操作成功!', Yii::app()->createUrl('admin/admin'));
      }else{
        $this->jsMsg($this->getError($result));
      }
    } else {
      if ($id > 0) {
        //编辑
        $data = User::model()->findByPk($id);
        $this->render('admin/post', array(
            'data' => $data
        ));
      } else {
        //添加
        $this->render('admin/post');
      }
    }
  }

  /**
   * 设置管理员状态
   * @param int $state
   */
  public function actionAdmin_gateway($state = 0)
  {
    echo UserService::instance()->setState($_POST['list'],$state)?1:0;
  }

  /**
   * 问题列表
   * @param string $keywords
   */
  public function actionIssue($keywords = '')
  {
    $this->iCheck();
    $data = IssueService::instance()->getList(20,true,0,$keywords);
    $this->render('issue/list',array(
      'data'=>$data
    ));
  }

  /**
   * 删除问题
   */
  public function actionDelete_issue()
  {
    echo IssueService::instance()->deleteIssue($this->filter($_POST['list']))?1:0;
  }

  /**
   * 主题列表
   * @param string $keywords
   */
  public function actionTopic($keywords = '')
  {
    $this->iCheck();
    $data = TopicService::instance()->getTopicList(20,true,$keywords);
    $this->render('topic/list', array(
        'data' => $data
    ));
  }

  /**
   * 删除主题
   */
  public function actionDelete_topic()
  {
    echo TopicService::instance()->delete($this->filter($_POST['list']))?1:0;
  }

  /**
   * 我的回复
   * @param string $keywords
   */
  public function actionThread($keywords = '')
  {
    $this->iCheck();
    $data = TopicService::instance()->getAllThreadList(20,$keywords);
    $this->render('thread/list', array(
        'data' => $data
    ));
  }

  /**
   * 删除回复
   */
  public function actionDelete_thread()
  {
    echo TopicService::instance()->deleteThread($this->filter($_POST['list']))?1:0;
  }

  /**
   * 回答列表
   */
  public function actionReply($keywords = '')
  {
    $this->iCheck();
    $data = IssueService::instance()->getAllReplys(20,$keywords);
    $this->render('reply/list', array(
        'data' => $data
    ));
  }

  /**
   * 删除回答
   */
  public function actionDelete_reply()
  {
    echo IssueService::instance()->deleteReply($this->filter($_POST['list']))?1:0;
  }
} 